//
//  JJShowPictureViewController.m
//  MyAnimations
//
//  Created by jxf on 15/12/24.
//  Copyright © 2015年 JJ.sevn. All rights reserved.
//

#import "JJShowPictureViewController.h"
#import "JJCollectionViewCell.h"
#import "JJShowDetailViewController.h"

@interface JJShowPictureViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UIImageView *bgView;
@property(nonatomic, strong) UIImageView * containerView;

@end

@implementation JJShowPictureViewController

- (UIImageView *)containerView
{
    if (!_containerView) {
        UIImageView *containerView = [[UIImageView alloc] initWithFrame:self.view.bounds];
        [self.view addSubview:containerView];
//        containerView.image = self.bgView.image;
        _containerView = containerView;
    }
    return _containerView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.collectionView registerClass:[JJCollectionViewCell class] forCellWithReuseIdentifier:@"pic"];
    self.collectionView.bounces = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    
}
- (IBAction)backHome:(UIBarButtonItem *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}


#pragma mark -
#pragma mark - UICollectionDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 6;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    JJCollectionViewCell *cell = [JJCollectionViewCell collectionViewCellWithCollectionView:collectionView andIndexPath:indexPath];
    
    return cell;
}

#pragma mark -
#pragma mark - UICollectionDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    JJShowDetailViewController *showVc = [[JJShowDetailViewController alloc] init];
    showVc.mainVc = self;
    
//    showVc.view.tag = 100;
//    self.view.tag = 99;
    self.containerView.image = self.bgView.image;
    [showVc.view addSubview:self.containerView];
    [self presentViewController:showVc animated:NO completion:^{
        UIImageView *imageV = [[UIImageView alloc] init];
        [showVc.view addSubview:imageV];
        UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
        for (UIView *view in cell.contentView.subviews) {
            if ([view isKindOfClass:[UIImageView class]]) {
                UIImageView *imageView = (UIImageView *)view;
                imageV.image = imageView.image;
            }
        }
        imageV.layer.position = showVc.view.center;
        [UIView animateWithDuration:2.0 delay:0 usingSpringWithDamping:0.55 initialSpringVelocity:0 options:kNilOptions animations:^{
            imageV.bounds = showVc.view.bounds;
        } completion:^(BOOL finished) {
//            NSLog(@"%zd", self.containerView.superview.tag);
            [self.containerView removeFromSuperview];
//            NSLog(@"%zd", self.containerView.superview.tag);
//            NSLog(@"%zd", showVc.view.subviews.count);

        }];
    }];
}

#pragma mark -
#pragma mark - modal退出动画
- (void)showDetailViewDismisWithAnimation:(UIImageView *(^)())animationBlock
{
    if (animationBlock) {
        UIImageView *imageV = animationBlock();
        [self.view addSubview:imageV];
        
        //核心动画
//        CATransition *anim = [CATransition animation];
//        anim.duration = 1.0;
//        anim.type = @"pageCurl";
//        [imageV.layer addAnimation:anim forKey:nil];
//        imageV.hidden = YES;
//        [self performSelector:@selector(removeView:) withObject:imageV afterDelay:anim.duration];
        
        // frame动画
        [UIView animateWithDuration:1.0 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:0 options:kNilOptions animations:^{
            imageV.bounds = CGRectMake(0, 0, 20, 35);
        } completion:^(BOOL finished) {
            [imageV removeFromSuperview];
        }];

    }
}

- (void)removeView:(UIView *)removeV;
{
    [removeV removeFromSuperview];
}




@end
